﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditInvoiceDetails : BasePage
    {
        InvoiceDetails invoiceDetails;
        int invoiceDetailsId;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            invoiceDetails = (InvoiceDetails)Session[Request.QueryString[KEY]];
            string oldFilter = invoiceDetails.Filter;

            invoiceDetailsId = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                invoiceDetails.Filter = InvoiceDetails.ColumnNames.ID + "=" + invoiceDetailsId.ToString();

            if (!IsPostBack && invoiceDetails.RowCount > 0 && mode != ADD_MODE)
                LoadInfo();

            invoiceDetails.Filter = oldFilter;
        }

        private void LoadInfo()
        {
            if (!invoiceDetails.IsColumnNull("ProductName"))
                editInvoiceDetails.ProductName = invoiceDetails.ProductName;
            if (!invoiceDetails.IsColumnNull("Discount"))
                editInvoiceDetails.Discount = invoiceDetails.Discount;
            if (!invoiceDetails.IsColumnNull("Quantity"))
                editInvoiceDetails.Quantity = invoiceDetails.Quantity;
            if (!invoiceDetails.IsColumnNull("GST"))
                editInvoiceDetails.GST = invoiceDetails.GST;
            if (!invoiceDetails.IsColumnNull("Total"))
                editInvoiceDetails.Total = invoiceDetails.Total;
            if (!invoiceDetails.IsColumnNull("UnitPrice"))
                editInvoiceDetails.UnitPrice = invoiceDetails.UnitPrice;
            if (!invoiceDetails.IsColumnNull(InvoiceDetails.ColumnNames.Cost))
                editInvoiceDetails.UnitCost = invoiceDetails.Cost;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if (invoiceDetails != null)
            {
                if(Save())
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
            }
        }

        private bool Save()
        {
            string oldFilter = invoiceDetails.Filter;
            if (mode == ADD_MODE)
            {
                invoiceDetails.AddNew();
                invoiceDetails.BranchID = Branches.CurrentBranchID;
                invoiceDetails.Deleted = false;
            }
            else
            {
                invoiceDetails.Filter = Categories.ColumnNames.ID + "=" + invoiceDetailsId;
            }

            Decimal oldGST = invoiceDetails.GST;

            //Products p = new Products();
            //p.LoadByPrimaryKey(invoiceDetails.ProductID);
            //if (p.Quantity < editInvoiceDetails.Quantity)
            //{
            //    invoiceDetails.Filter = oldFilter;
            //    ((Popup)Master).ShowErrorMessage("Out of stock!");
            //    LoadInfo();
            //    return false;
            //}

            invoiceDetails.Quantity = editInvoiceDetails.Quantity;
            if (SessionUser.Purchase)
            {
                invoiceDetails.UnitPrice = editInvoiceDetails.UnitPrice;
                invoiceDetails.Discount = editInvoiceDetails.Discount;
            }
            invoiceDetails.Total = Math.Round(invoiceDetails.Quantity * invoiceDetails.UnitPrice - invoiceDetails.Discount, 2);
            invoiceDetails.GST = oldGST == 0 ? 0 : Math.Round(invoiceDetails.Total * SysVars.GST / (SysVars.GST + 100), 2);

            invoiceDetails.LastModifiedByUser = SessionUser.ID;
            invoiceDetails.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);

            //invoiceDetails.Save();
            invoiceDetails.Filter = oldFilter;
            ((Popup)Master).ShowMessage("Saved");
            return true;
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (invoiceDetails != null)
            {
                if(Save())
                    CloseDlg(1);
            }
        }
    }
}
